package com.ufgov.smartclient.component.table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicLabelUI;
import javax.swing.table.JTableHeader;

/**
 * <p>Title: Smart Client(OSGI)</p>
 * <p>Description: TableRowHeader渲染器</p>
 * <p>Copyright: Copyright 2008 ufgov, Inc.</p>
 * <p>Company: ufgov</p>
 * <p>创建时间: 2008-4-16</p>
 * @author 刘永伟(manlge)
 * @version 1.0
 */
public class RowHeaderRenderer extends JPanel implements ListCellRenderer {

  /**
   * 显示行号的Label
   */
  protected JLabel label;

  protected JLabel createLabel() {
    return new JLabel() {
      @Override
      public void updateUI() {
        setUI(new BasicLabelUI());
      };
    };
  }

  /**
   * 显示选定状态的CheckBox
   */
  protected JCheckBox checkBox = new JCheckBox();

  /**
   * 表格组件
   */
  protected JGroupableTable table;

  /**
   * 空的icon组件，主要显示一个占位符
   */
  protected Icon selectedIcon = new ImageIcon(RowHeaderRenderer.class
    .getResource("cursor.gif"));

  protected transient Icon nullIcon = new Icon() {

    @Override
    public int getIconHeight() {
      return 9;
    }

    @Override
    public int getIconWidth() {
      return 5;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
    }
  };

  public RowHeaderRenderer(JGroupableTable table) {
    this.label = createLabel();
    this.table = table;
    JTableHeader header = table.getTableHeader();
    label.setHorizontalAlignment(SwingConstants.RIGHT);
    label.setHorizontalTextPosition(SwingConstants.LEFT);
    label.setForeground(header.getForeground());
    label.setBackground(header.getBackground());
    label.setFont(header.getFont());
    setLayout(new BorderLayout());
    add(label, BorderLayout.CENTER);
    add(checkBox, BorderLayout.EAST);

    label.setOpaque(true);
    checkBox.setOpaque(true);
    this.setOpaque(true);
  }

  @Override
  public Dimension getPreferredSize() {
    Dimension size = super.getPreferredSize();
    return size;
  }

  public Component getListCellRendererComponent(JList list, Object value, int index,
    boolean isSelected, boolean cellHasFocus) {
    int tableRowHeaderWidth = 12;
    TableRowHeader tableRowHeader = table.getTableRowHeader();
    RowHeaderModel rowHeaderModel = tableRowHeader.getRowHeaderModel();
    label.setText(rowHeaderModel.getRowTitle(index));
    Dimension size = new Dimension();
    size.width = tableRowHeaderWidth;
    if (index > table.getRowCount() - 1) {
      size.height = table.getTotalLineHeight();
    } else {
      size.height = table.getRowHeight(index);
    }
    setPreferredSize(size);
    if (isSelected) {
      label.setIcon(selectedIcon);
      //      setBackground(table.getSelectionBackground());
      //      setForeground(table.getSelectionForeground());
    } else {
      label.setIcon(nullIcon);
      //      setBackground(table.getTableHeader().getBackground());
      //      setForeground(table.getTableHeader().getForeground());
    }
    checkBox.setBorder(null);
    checkBox.setSelected((Boolean) value);
    checkBox.setVisible(table.getTableRowHeader().isShowCheckedColumn());
    checkBox.setBackground(rowHeaderModel.getCheckBoxBackground(index));
    checkBox.setEnabled((rowHeaderModel.isCheckable(index) && index < table
      .getRowCount())
      && (table.getTableRowHeader().isCheckEnabled()));
    Color background = table.getTableHeader().getBackground();
    label.setBackground(background);
    this.setBackground(background);
    label.setBorder(UIManager.getBorder("TableHeader.cellBorder"));

    return this;
  }

}
